﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FF.Entity;
using FF.Method;

namespace FF
{
    public partial class FormTypeManager : Form
    {
         const int ACTION_TYPE_ADD = 1;
         const int ACTION_TYPE_UPDATE = 2;
         const int ACTION_TYPE_DELETE = 3;
        public const int FORM_TYPE_USER = 1;
         public const int FORM_TYPE_CUSTOMER = 2;
        private int actionType;
        private int formType;//判断是管理用户类型还是客户类型
        public FormTypeManager()
        {
            InitializeComponent();
        }

        public FormTypeManager(int formType)
        {
            InitializeComponent();
            this.formType = formType;
        }

        private void FormUserType_Load(object sender, EventArgs e)
        {
            this.Text = (formType == FORM_TYPE_USER ? "用户类型管理" : "客户类型管理");
            DataSet dataset = (formType == FORM_TYPE_USER ? SQLiteHelper.Query("select * from ff_user_type") : SQLiteHelper.Query("select * from ff_customer_type"));
            dataGridView1.DataSource = dataset.Tables[0];
            toolStripButtonCancel.Enabled = false;
            toolStripButtonSave.Enabled = false;
            //toolStripButtonAdd.Enabled = false;
            //toolStripButtonUpdate.Enabled = false;
            //toolStripButtonDelete.Enabled = false;
            textBoxCode.Enabled = false;
            textBoxName.Enabled = false;
            textBoxRemark.Enabled = false;
            textBoxDiscount.Enabled = false;
            if(formType == FORM_TYPE_USER)
            {
                textBoxDiscount.Visible = false;
                labelDiscount.Visible = false;
                //dataGridView1.Columns[3].Visible = false;  //隐藏折扣率列
                dataGridView1.Columns.Remove("discount");
            }
        }

        private void setButtonStatusToSaveMode()
        {
            toolStripButtonUpdate.Enabled = false;
            toolStripButtonDelete.Enabled = false;
            toolStripButtonAdd.Enabled = false;

            toolStripButtonCancel.Enabled = true;
            toolStripButtonSave.Enabled = true;

            //textBoxCode.Enabled = true;
            //textBoxName.Enabled = true;
            //textBoxRemark.Enabled = true;
            //textBoxCode.Text = "";
            //textBoxName.Text = "";
            //textBoxRemark.Text = "";
        }

        private void setButtonStatusToChooseMode()
        {
            toolStripButtonUpdate.Enabled = true;
            toolStripButtonDelete.Enabled = true;
            toolStripButtonAdd.Enabled = true;

            toolStripButtonCancel.Enabled = false;
            toolStripButtonSave.Enabled = false;

            //textBoxCode.Text = "";
            //textBoxName.Text = "";
            //textBoxRemark.Text = "";
            textBoxCode.Enabled = false;
            textBoxName.Enabled = false;
            textBoxRemark.Enabled = false;
            textBoxDiscount.Enabled = false;
        }

        private void toolStripButtonAdd_Click(object sender, EventArgs e)
        {
            setButtonStatusToSaveMode();
            toolStripButtonAdd.Enabled = true;
            textBoxCode.Enabled = true;
            textBoxName.Enabled = true;
            textBoxRemark.Enabled = true;
            textBoxDiscount.Enabled = true;
            actionType = ACTION_TYPE_ADD;

            textBoxCode.Text = "";
            textBoxName.Text = "";
            textBoxRemark.Text = "";
            textBoxDiscount.Text = "";
        }

        private void toolStripButtonCancel_Click(object sender, EventArgs e)
        {
            setButtonStatusToChooseMode();
        }

        private void toolStripButtonSave_Click(object sender, EventArgs e)
        {
            switch(actionType)
            //if(toolStripButtonAdd.Enabled)
            {
                case ACTION_TYPE_ADD:
                    {
                        if (textBoxCode.Text == "" || textBoxName.Text == "")
                        {
                            MessageBox.Show("类型代码或名称不能为空！", "提示信息 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                        if (formType == FORM_TYPE_USER)
                        {
                            if (UserTypeMethod.getUserTypeByTypeCode(textBoxCode.Text.Trim()) != null)
                            {
                                MessageBox.Show("类型代码已经存在，请更换！", "提示信息 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                textBoxCode.Focus();
                                return;
                            }
                            UserType ut = new UserType();
                            ut.type = textBoxCode.Text.Trim();
                            ut.name = textBoxName.Text.Trim();
                            ut.remark = textBoxRemark.Text.Trim();
                            if (UserTypeMethod.addUserType(ut) == 1)
                            {
                                MessageBox.Show("添加成功！");
                                setButtonStatusToChooseMode();
                            }
                            else
                            {
                                MessageBox.Show("添加失败！");
                                setButtonStatusToChooseMode();
                            }
                        }else  //add customer type
                        {
                            if (CustomerTypeMethod.getTypeByTypeCode(textBoxCode.Text.Trim()) != null)
                            {
                                MessageBox.Show("类型代码已经存在，请更换！", "提示信息 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                textBoxCode.Focus();
                                return;
                            }
                            CustomerType ut = new CustomerType();
                            ut.type = textBoxCode.Text.Trim();
                            ut.name = textBoxName.Text.Trim();
                            ut.remark = textBoxRemark.Text.Trim();
                            ut.discount = textBoxDiscount.Text.Trim();
                            if (CustomerTypeMethod.addType(ut) == 1)
                            {
                                MessageBox.Show("添加成功！");
                                setButtonStatusToChooseMode();
                            }
                            else
                            {
                                MessageBox.Show("添加失败！");
                                setButtonStatusToChooseMode();
                            }
                        }
                        updateDataGridView();
                        break;
                    }
                case ACTION_TYPE_UPDATE:
                   {
                        if (textBoxCode.Text == "" || textBoxName.Text == "")
                        {
                            MessageBox.Show("类型代码或名称不能为空！", "提示信息 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                        if (formType == FORM_TYPE_USER)  //update user type
                        {
                            UserType ut = new UserType();
                            ut.type = textBoxCode.Text.Trim();
                            ut.name = textBoxName.Text.Trim();
                            ut.remark = textBoxRemark.Text.Trim();
                            if (UserTypeMethod.updateUserType(ut) == 1)
                            {
                                MessageBox.Show("修改成功！");
                                setButtonStatusToChooseMode();
                            }
                            else
                            {
                                MessageBox.Show("修改失败！");
                                setButtonStatusToChooseMode();
                            }
                        }
                        else{  //update customer type
                            CustomerType ut = new CustomerType();
                            ut.type = textBoxCode.Text.Trim();
                            ut.name = textBoxName.Text.Trim();
                            ut.remark = textBoxRemark.Text.Trim();
                            ut.discount = textBoxDiscount.Text.Trim();
                            if (CustomerTypeMethod.updateType(ut) == 1)
                            {
                                MessageBox.Show("修改成功！");
                                setButtonStatusToChooseMode();
                            }
                            else
                            {
                                MessageBox.Show("修改失败！");
                                setButtonStatusToChooseMode();
                            }
                        }
                        updateDataGridView();
                       break;
                   }
                case ACTION_TYPE_DELETE:
                    {
                        if (textBoxCode.Text == "")
                        {
                            MessageBox.Show("请选择要删除的记录！", "提示信息 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                        if (formType == FORM_TYPE_USER)
                        {
                            string sql = "select * from ff_user where type='" + textBoxCode.Text.Trim() + "'";
                            //ds.Tables[0].Rows.Count
                            DataSet ds = SQLiteHelper.Query(sql);
                            if (ds.Tables[0].Rows.Count >= 1)
                            {
                                MessageBox.Show("此类型正在使用，不能删除！", "提示信息 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                return;
                            }
                            if (UserTypeMethod.deleteUserType(textBoxCode.Text.Trim()) == 1)
                            {
                                MessageBox.Show("删除成功！");

                            }
                            else
                            {
                                MessageBox.Show("删除失败！");
                            }
                        }else
                        {
                            string sql = "select * from ff_customer where type='" + textBoxCode.Text.Trim() + "'";
                            //ds.Tables[0].Rows.Count
                            DataSet ds = SQLiteHelper.Query(sql);
                            if (ds.Tables[0].Rows.Count >= 1)
                            {
                                MessageBox.Show("此类型正在使用，不能删除！", "提示信息 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                return;
                            }
                            if (CustomerTypeMethod.deleteType(textBoxCode.Text.Trim()) == 1)
                            {
                                MessageBox.Show("删除成功！");

                            }
                            else
                            {
                                MessageBox.Show("删除失败！");
                            }
                        }
                        setButtonStatusToChooseMode();
                        updateDataGridView();
                        break;
                    }
                default:
                    {
                       
                        break;
                    }
                   
             }
        }

        private  void updateDataGridView()
        {
            //修改数据后，重新绑定数据，刷新
            //DataSet dataset = SQLiteHelper.Query("select * from ff_user_type");
            DataSet dataset = (formType == FORM_TYPE_USER ? SQLiteHelper.Query("select * from ff_user_type") : SQLiteHelper.Query("select * from ff_customer_type"));
            dataGridView1.DataSource = dataset.Tables[0];
        }

        private void toolStripButtonReturn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void toolStripButtonUpdate_Click(object sender, EventArgs e)
        {
            setButtonStatusToSaveMode();
            toolStripButtonUpdate.Enabled = true;
            textBoxCode.Enabled = false;
            textBoxName.Enabled = true;
            textBoxRemark.Enabled = true;
            textBoxDiscount.Enabled = true;
            actionType = ACTION_TYPE_UPDATE;
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //if(actionType == ACTION_TYPE_UPDATE || actionType == ACTION_TYPE_DELETE)
            //{
            if (formType == FORM_TYPE_USER)
            {
                UserType UserType = UserTypeMethod.getUserTypeByTypeCode(this.dataGridView1[0, this.dataGridView1.CurrentCell.RowIndex].Value.ToString());
                if (UserType != null)
                {
                    textBoxCode.Text = UserType.type;
                    textBoxName.Text = UserType.name;
                    textBoxRemark.Text = UserType.remark;
                }
            }else{
                CustomerType UserType = CustomerTypeMethod.getTypeByTypeCode(this.dataGridView1[0, this.dataGridView1.CurrentCell.RowIndex].Value.ToString());
                if (UserType != null)
                {
                    textBoxCode.Text = UserType.type;
                    textBoxName.Text = UserType.name;
                    textBoxRemark.Text = UserType.remark;
                    textBoxDiscount.Text = UserType.discount;
                }
            }
            //}
        }

        private void toolStripButtonDelete_Click(object sender, EventArgs e)
        {
            setButtonStatusToSaveMode();
            toolStripButtonDelete.Enabled = true;
            textBoxCode.Enabled = false;
            textBoxName.Enabled = false;
            textBoxRemark.Enabled = false;
            actionType = ACTION_TYPE_DELETE;
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
            {
                MessageBox.Show("请输入数字");
                e.Handled = true;
            }
        }
    }
}
